package com.xiaomaoguai.meizitu;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.cookie.CookieSpecProvider;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.impl.cookie.DefaultCookieSpecProvider;

/**
 * @Description :
 * @FileName: AbstractHttpUtils.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年6月25日 下午10:08:12
 * @Version:V1.00
 */
public abstract class AbstractHttpUtils {

	protected final HttpClient defaultHttpClient = HttpClientBuilder.create().build();

	protected final static String USER_AGENT = "Mozilla/5.0";

	protected CookieStore cookieStore = null;

	protected HttpClientContext context = null;

	protected Map<String, String> cookieMap = null;

	protected HttpClient intiHttpClient() {
		return defaultHttpClient;
	}

	protected void doGetWithCookie(String url) {
		HttpGet httpGet = new HttpGet(url);
		httpGet.setHeader(HttpHeaders.USER_AGENT, USER_AGENT);
		try {
			/**
			 * 访问主页得到Cookie，设置Cookie后再放问
			 */
			HttpResponse response = defaultHttpClient.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
				setCookieStore(response);
				setContext();
			} else {
				System.out.println("返回错误...");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void setCookieStore(HttpResponse response) {
		cookieStore = new BasicCookieStore();
		Header[] headers = response.getHeaders("Set-Cookie");
		getCookieMap(headers);
		String cookie = "";
		for (Header header : headers) {
			cookie += header.getValue();
		}

		System.out.println("服务器返回Cookie : " + cookie);

		BasicClientCookie clientCookie = new BasicClientCookie("Cookie", cookie);
		// clientCookie.setVersion(0);
		// cookie.setAttribute(ClientCookie.VERSION_ATTR, "0");
		// cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "127.0.0.1");
		// cookie.setAttribute(ClientCookie.PORT_ATTR, "8080");
		// cookie.setAttribute(ClientCookie.PATH_ATTR, "/CwlProWeb");
		cookieStore.addCookie(clientCookie);
	}

	protected void getCookieMap(Header[] headers) {
		cookieMap = new HashMap<String, String>();
		for (Header header : headers) {
			cookieMap.put(header.getName(), header.getValue());
		}
	}

	protected HttpClientContext setContext() {
		context = HttpClientContext.create();
		Registry<CookieSpecProvider> registry = RegistryBuilder.<CookieSpecProvider> create()
				.register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider()).build();
		context.setCookieSpecRegistry(registry);
		context.setCookieStore(cookieStore);
		return context;
	}

}
